
import { Base } from 'base.js';
import { Config } from 'config.js';

class Address extends Base {
  constructor() {
    super();
  }
  // 格式化地址信息
  setAddressInfo(res){
    var province = res.provinceName || res.province,
      city = res.cityName || res.city,
      country = res.countyName || res.country,
      detail = res.detailInfo || res.detail;

    var totalDetail = city + country + detail;

    if (!this.isCenterCity(province)) {
      totalDetail = province + totalDetail;
    };

    return totalDetail;
  }

  /*获得我自己的收货地址*/
  getAddress(callback, session3rd) {
    var that = this;
    var param = {
      url: `getAddress?session3rd=${session3rd}`,
      sCallback: function (res) {
        if (res.status) {
          callback && callback(res);
        }
      }
    };
    this.request(param);
  }

  /*是否为直辖市*/
  isCenterCity(name) {
    var centerCitys = ['北京市', '天津市', '上海市', '重庆市'],
      flag = centerCitys.indexOf(name) >= 0;
    return flag;
  }

  /*更新保存地址*/
  submitAddress(data, callback) {
    var param = {
      url: '/post/addAddress',
      type: 'POST',
      data: data,
      sCallback: function (res) {
        if (res.status){
          callback && callback(res);
        }
      }, eCallback(res) {
        callback && callback(res);
      }
    };
    this.request(param);
  }

  /*保存地址*/
  // _setUpAddress(res) {
  //   var formData = {
  //     name: res.userName,
  //     province: res.provinceName,
  //     city: res.cityName,
  //     country: res.countyName,
  //     mobile: res.telNumber,
  //     detail: res.detailInfo
  //   };
  //   return formData;
  // }
}

export {Address}